package com.example.beson.androiddemo;

import android.app.Dialog;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.example.beson.androiddemo.utils.CustomToast;
import com.example.beson.androiddemo.utils.DialogUtils;
import com.example.beson.androiddemo.utils.SuperTextView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import cn.aigestudio.datepicker.bizs.calendars.DPCManager;
import cn.aigestudio.datepicker.bizs.decors.DPDecor;
import cn.aigestudio.datepicker.cons.DPMode;
import cn.aigestudio.datepicker.views.DatePicker;


public class ScrollViewTestActivity extends AppCompatActivity {
    private DatePicker test_datapicker;
    List<String> datalist = new ArrayList<>();
    private SuperTextView supertextview1;

    private Dialog MyDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scroll_view_test);
        findViewById(R.id.scroll_textview).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(ScrollViewTestActivity.this,"scroll_textview",Toast.LENGTH_LONG).show();
            }
        });

        test_datapicker= (DatePicker) findViewById(R.id.test_datapicker);
        supertextview1= (SuperTextView) findViewById(R.id.supertextview1);
        supertextview1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                i++;
                CustomToast.showToast(ScrollViewTestActivity.this,"SuperTextView"+i);
            }
        });
        myCalendar();
    }
    int i=0;
    private void myCalendar() {
        MyDialog= DialogUtils.CreateDialog(ScrollViewTestActivity.this);
        MyDialog.show();
        mHandler.sendEmptyMessageDelayed(1,2000);
        datalist.add("2016-4-10");
        datalist.add("2017-4-10");
        datalist.add("2017-4-15");
        datalist.add("2017-4-16");
        datalist.add("2017-4-19");
        datalist.add("2017-4-22");
        datalist.add("2017-4-22");
        datalist.add("2017-4-24");
        datalist.add("2017-4-27");
        datalist.add("2017-4-29");
        datalist.add("2017-5-20");
        datalist.add("2018-4-10");
        DPCManager.getInstance().setDecorBG(datalist);
        test_datapicker.setFestivalDisplay(true); //是否显示节日
        test_datapicker.setHolidayDisplay(false); //是否显示假期
        test_datapicker.setDeferredDisplay(false); //是否显示补休
        test_datapicker.setTodayDisplay(true);

        Calendar c = Calendar.getInstance();//首先要获取日历对象
        int mYear = c.get(Calendar.YEAR); // 获取当前年份
        final int mMonth = c.get(Calendar.MONTH) + 1;// 获取当前月份
        test_datapicker.setDate(mYear, mMonth);
        test_datapicker.setDPDecor(new DPDecor() {
            @Override
            public void drawDecorBG(Canvas canvas, Rect rect, Paint paint) {
                paint.setColor(Color.RED);
                paint.setAntiAlias(true);
                Resources res=getResources();
                Bitmap mBitmap = BitmapFactory.decodeResource(res,R.mipmap.yqiandao_icon);
                canvas.drawBitmap(mBitmap, rect.centerX() - mBitmap.getWidth() / 2f, rect.centerY() - mBitmap.getHeight() / 2f, paint);
            }
        });
        test_datapicker.setMode(DPMode.SINGLE);
        test_datapicker.setOnDatePickedListener(new DatePicker.OnDatePickedListener() {
            @Override
            public void onDatePicked(String date) {
                int belcount=0;
                for(int i=0;i<datalist.size();i++){
                    if(datalist.get(i).toString().equals(date)){
                        belcount++;
                    }
                }
                if(belcount>0){
                    if(belcount==1){
                        Toast.makeText(ScrollViewTestActivity.this, date+"   跳转备忘录详情", Toast.LENGTH_LONG).show();
                    }else{
                        Toast.makeText(ScrollViewTestActivity.this, date+"   有多个备忘录,可以跳转一个列表", Toast.LENGTH_LONG).show();
                    }
                }else{
                    Toast.makeText(ScrollViewTestActivity.this, date+"   跳转添加备忘录", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
    private Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            MyDialog.hide();
            super.handleMessage(msg);
        }
    };
    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyDialog.dismiss();
    }
}
